<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e8eb 100%);
        }
        .login-container {
            background-color: rgba(255, 255, 255, 0.95);
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 320px;
            backdrop-filter: blur(10px);
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-size: 14px;
        }
        input {
            width: 100%;
            padding: 10px;
            margin-top: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            transition: border-color 0.3s;
        }
        input:focus {
            border-color: #4CAF50;
            outline: none;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s;
        }
        button:hover {
            background-color: #45a049;
            transform: translateY(-1px);
        }
        .error-message {
            color: #ff4d4f;
            margin-top: 10px;
            text-align: center;
            display: none;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <form onsubmit="login(event)">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="admin 或 user" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="error-message" id="loginError">用户名或密码错误</div>
            <button type="submit">登录</button>
        </form>
    </div>

    <script>
        async function login(event) {
            // 阻止表单默认提交行为
            event.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            console.log('尝试登录:', username);
            
            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });
                
                console.log('登录响应状态:', response.status);
                
                if (response.ok) {
                    const data = await response.json();
                    console.log('登录响应数据:', data);
                    
                    // 根据用户角色处理导航栏
                    if (data.role === 'user') {
                        // 普通用户登录成功后，将配置页面从localStorage中移除
                        console.log('设置用户角色为: user');
                        localStorage.setItem('userRole', 'user');
                    } else if (data.role === 'admin') {
                        // 管理员登录成功后，确保配置页面可访问
                        console.log('设置用户角色为: admin');
                        localStorage.setItem('userRole', 'admin');
                    } else {
                        console.warn('未知的用户角色:', data.role);
                        // 默认设置为user
                        localStorage.setItem('userRole', 'user');
                    }
                    
                    // 保存用户名
                    localStorage.setItem('username', data.username);
                    
                    // 确认localStorage设置成功
                    console.log('localStorage设置后检查:');
                    console.log('userRole =', localStorage.getItem('userRole'));
                    console.log('username =', localStorage.getItem('username'));
                    
                    // 检查Cookie是否设置成功
                    console.log('检查Cookie:');
                    document.cookie.split(';').forEach(cookie => {
                        console.log(cookie.trim());
                    });
                    
                    // 跳转到欢迎页面
                    window.location.href = '/welcome.html';
                } else {
                    console.error('登录失败，状态码:', response.status);
                    document.getElementById('loginError').style.display = 'block';
                }
            } catch (error) {
                console.error('登录失败:', error);
                document.getElementById('loginError').style.display = 'block';
            }
        }
    </script>
</body>
</html>